package com.lanou.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.entity.Consultant;
import com.lanou.entity.Course;
import com.lanou.service.ConsultantService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consultant")
public class ConsultantController {

    @Autowired
    private ConsultantService consultantService;

    @PostMapping
    public R insertConsultant(@RequestBody Consultant consultant) {
        int rows = consultantService.insertConsultant(consultant);
        return rows > 0 ? R.ok("咨询师新增成功") : R.fail("咨询师新增失败");
    }

    @DeleteMapping("/{id}")
    public R deleteConsultantById(@PathVariable Integer id) {
        int rows = consultantService.deleteConsultantById(id);
        return rows > 0 ? R.ok("咨询师删除成功") : R.fail("咨询师删除失败");
    }


    @DeleteMapping("/batch")
    public R deleteConsultantsByIds(@RequestBody List<Integer> ids) {
        int rows = consultantService.deleteConsultantsByIds(ids);
        return rows > 0 ? R.ok("成功删除 " + rows + " 条咨询师数据") : R.fail("批量删除失败");
    }


    @PutMapping
    public R updateConsultant(@RequestBody Consultant consultant) {
        int rows = consultantService.updateConsultant(consultant);
        return rows > 0 ? R.ok("咨询师更新成功") : R.fail("咨询师更新失败");
    }


    @GetMapping("/{id}")
    public R selectConsultantById(@PathVariable Integer id) {
        Consultant consultant = consultantService.selectConsultantById(id);
        if (consultant == null) {
            return R.fail("未查询到ID为 " + id + " 的咨询师");
        }
        return R.ok(consultant);
    }


    @GetMapping("/page")
    public R selectConsultantByPage(
            Consultant consultant,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {

        PageInfo<Consultant> pageInfo = consultantService.selectConsultantByPage(consultant, pageNum, pageSize);
        return R.ok(pageInfo);
    }

    @GetMapping("/level/{levelId}")
    public R selectConsultantsByLevelId(@PathVariable Integer levelId) {
        List<Consultant> consultants = consultantService.selectConsultantsByLevelId(levelId);
        return R.ok(consultants);
    }


    @GetMapping("/tag/{tagId}")
    public R selectConsultantsByTagId(@PathVariable Integer tagId) {
        List<Consultant> consultants = consultantService.selectConsultantsByTagId(tagId);
        return R.ok(consultants);
    }

    @GetMapping("/{consultantId}/course")
    public R selectCoursesByConsultantId(@PathVariable Integer consultantId) {
        List<Course> courses = consultantService.selectCoursesByConsultantId(consultantId);
        return R.ok(courses);
    }

    @PostMapping("/filter")
    public R selectByFilter(@RequestBody Map<String, Object> filterMap) {
        try {
            String region = (String) filterMap.get("region");
            List<Integer> tagIds = (List<Integer>) filterMap.get("tags");
            String levelName = (String) filterMap.get("level");
            String gender = (String) filterMap.get("gender");


            // 调用 Service 方法查询
            List<Consultant> consultants = consultantService.selectConsultantsByFilter(
                    region,
                    tagIds,
                    levelName,
                    gender

            );
            return R.ok(consultants);
        } catch (
                Exception e) {
            e.printStackTrace();
            return R.fail("筛选咨询师失败，请稍后重试");
        }
    }

    //根据咨询师等级和咨询类别级联查询
    @GetMapping("/levelName/sort")
    public R selectConsultantsByLevelSort(@RequestParam("levelName") String levelName,
                                          @RequestParam("sort") String sort) {
        List<Consultant> consultants = consultantService.selectConsultantByLevelAndSort(levelName, sort);
        return R.ok(consultants);

    }
}